﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Xsl;
using System.IO;
using System.Xml;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace XSLTListitemWebPart
{
    public class XSLTHelper
    {
        private const string NODENAME_LISTITEMS = "listitems";
        private const string NODENAME_LISTITEM = "listitem";
        private const string NODENAME_NOLISTITEMS = "nolistitems";

        public static void Transform(string Xslt, List<SPListItem> listitems, XsltArgumentList argumentlist, TextWriter output)
        {
            XslCompiledTransform transform = new XslCompiledTransform();
            System.Xml.XmlTextReader xsltreader = new System.Xml.XmlTextReader(new StringReader(Xslt));
            try
            {
                transform.Load(xsltreader);
            }
            finally
            {
                xsltreader.Close();
            }

            XmlDocument document = new System.Xml.XmlDocument();

            // Do we have listitems to render?
            if (listitems.Count > 0)
            { // Yes
                XmlElement xmle_listitems = document.CreateElement(NODENAME_LISTITEMS);
                document.AppendChild(xmle_listitems);

                foreach (SPListItem listitem in listitems)
                {
                    XmlElement xmle_listitem = document.CreateElement(NODENAME_LISTITEM);
                    xmle_listitems.AppendChild(xmle_listitem);

                    #region Iterate fields of the listitem
                    foreach (SPField listitemfield in listitem.Fields)
                    {
                        try
                        {
                            XmlAttribute xmla_field = document.CreateAttribute(listitemfield.InternalName);

                            object fieldvalue = listitem[listitemfield.Id];
                            if (fieldvalue != null)
                            {
                                string fieldvaluetext = listitemfield.GetFieldValueAsHtml(fieldvalue);
                                xmla_field.InnerText = fieldvaluetext;
                            }
                            xmle_listitem.Attributes.Append(xmla_field);
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine(string.Format("Failed to retrieve the field '{0}' from listitem '{1}'.\r\n{2}", listitemfield.StaticName, listitem.Url, ex.ToString()));
                        }
                    }
                    #endregion
                }
            }
            else
            { // No
                XmlElement xmle_nolistitems = document.CreateElement(NODENAME_NOLISTITEMS);
                document.AppendChild(xmle_nolistitems);
            }
            
            transform.Transform(document, argumentlist, output);
        }
    }
}
